<?php

chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";



class MetalsPlugin extends IPlugin 
{                   
    var $color;
    
    function MetalsPlugin($parent = null)
    {
        IPlugin::IPlugin($parent);
        $this->tplfolder = dirname(__FILE__)."/tpl";
        $this->color = "darkgray";
    }
                                     
    function getPluginTag() { return "metalslist"; }
    function getPluginName() { return "Metals List Plugin"; }
    function getPluginVersion() { return "1.0.0"; }
    
    function getProperty($p = null)
    {
        if(null == $p)
        {
            return array("MenuBlockTitle", "MenuTitle");
        }
        $result = "";
        switch ($p)
        {
            case "MenuBlockTitle": { return "Site"; } break;
            case "MenuTitle" : { return "Items Metals"; } break;
        };
        return $result;
    }
    
    // ----------------------------------------------------------------------------------
    // --- [ Handling all actions ] -----------------------------------------------------
    // ----------------------------------------------------------------------------------

    function doAction($params)
    {
        if(null != ($result = IPlugin::doAction($params)))
        {
            return $result;
        }

        $arg = getCoreInstance();
        $arg = $arg->getGET();
        
        if("echo" == $arg["action"])
        {
            echo "MetalsList: echo: ".$arg["string"];
        }

        if("doSubmitEditNewsline" == $_POST["action"])
        {
            $z = $_POST;
            $uid = $z["id"];
            unset($z["action"], $z["id"]);
            
            $sql = "";
            unset($z['active']);
            foreach ($z as $k => $v)
            {
                $sql .= ($sql == "" ? "" : ", ") . "{$k} = '" . str2db($v) . "'";
            }
            $core = getCoreInstance();
            $db = $core->getProperty("db");
            
            $sql = "update metal set {$sql} where metal_name ='{$uid}'";
            
            $db->Exec($sql);
        }

        if("doSubmitAddNewsline" == $_POST["action"])
        {
            $z = $_POST;
            unset($z["action"], $z["id"]);
            
            $sql_rows = "";
            $sql_values = "";
            unset($z['active']);

            foreach ($z as $k => $v)
            {
                $sql_rows .= ($sql_rows == "" ? "" : ", ") . $k;
                $sql_values .= ($sql_values == "" ? "" : ", ") . "'" . str2db($v) . "'";
            }
            
            $core = getCoreInstance();
            $db = $core->getProperty("db");
            
            $sql = "INSERT INTO metal ({$sql_rows}) VALUES ({$sql_values})";
            
            $db->Exec($sql);
        }

        return true;
    }


    // ----------------------------------------------------------------------------------
    // --- [ Render pages ] -------------------------------------------------------------
    // ----------------------------------------------------------------------------------

    function Render($params)
    {
        global $_G;
        if(null != ($result = IPlugin::Render($params)))
        {
            return $result;
        }
        
        $core = getCoreInstance();
        $render = $core->framework->smarty;
        $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

        $page = "";

        switch ($_G["page"]) {
            case "edit":
                $page = $this->renderNewslineEdit($render);
                break;
            case "delete":
                $this->renderNewslineDelete($render);
                $page = $this->renderNewslineList($render);
                break;
            default:
                $page = $this->renderNewslineList($render);
                break;
        }

        return $render->fetch("file:".$this->tplfolder."/".$page);
    }
    
    
    // ----------------------------------------------------------------------------------
    // --- [ Generate Metals    List ] --------------------------------------------------
    // ----------------------------------------------------------------------------------
    
    function cb_rndOS($data)
    {
        $dt = array(
                array($data["metal_name"], "align=\"center\" valign=\"top\""),
                array("<a href=\"?".$this->getURL()."&page=edit&id={$data[metal_name]}\">edit</a>&nbsp;&nbsp;<a href=\"?".$this->getURL()."&page=delete&id={$data[metal_name]}\">delete</a>", "align=\"center\"")

                );
        return $dt;
    }

    function renderNewslineList(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        
        $pg = &$core->getSnippet("paginator");
        $params = array(
                    "cb_class" => &$this,
                    "cb_func" => cb_rndOS,
                    "urlbase" => $this->getURL(),
                    "caption" => "Metals list",
                    "emptymsg" => "No Metals added yet",
                    "header" => array("Metal", "Actions"),
                    "items" => "",
                    "source" => "sql", // -- sql, array
                    "sql_items" => "select metal_name from metal ORDER BY metal_id DESC",
                    "sql_count" => "select count(*) from metal",
                    "ippg" => 10, // -- items per page
                    "nppv" => 5, // -- number pages per view
                    );
        $HTML = $pg->Render($params);
        $render->assign("metals", $HTML);

        return "metals.tpl.php";
    }
    
    function renderNewslineEdit(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        
        $uid = $_GET["id"];
        if(!isset($_GET["id"]) || $uid<0)
        {
            header("location: ./?".$this->getURL());
            exit();
        }
        
        $render->assign("url", "?".$this->getURL());
        $render->assign("id", $uid);
        $news = $db->getRow("select * from metal where metal_name=\"{$uid}\"");
// -------------------------------      
//$sBasePath = $_SERVER['PHP_SELF'] ;
//$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "_samples" ) ) ;
$sBasePath = "/lib/fckeditor/";

$oFCKeditor = new FCKeditor('text_news') ;
$oFCKeditor->BasePath   = $sBasePath ;
$oFCKeditor->ToolbarSet = "Default"; // Optimum
$oFCKeditor->Height     = 450;
$oFCKeditor->Value      = db2str($news["text_news"]);
//$oFCKeditor->Create() ;
// -------------------------------
        $render->assign("texteditor", $oFCKeditor->CreateHtml());
        
        $render->assign("news", $news);

        return "metaledit.tpl.php";
    }
    
    function renderNewslineDelete(&$render)
    {
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        
        $uid = $_GET["id"];
        if(empty($uid) || $uid<0)
        {
            header("location: ./?".$this->getURL());
            exit();
        }
        
        $sql = "DELETE FROM metal WHERE metal_name = '$uid'";
        $db->Exec($sql);
    }
};


?>